﻿using System;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
using ApplicationLibrary;

namespace ApplicationManager
{
    public partial class TreeMenu : UserControl
    {
        public event EventHandler NodeClick;

        private TreeNode currentNode;

        public TreeMenu()
        {
            InitializeComponent();
        }

        public void AddButton(ApplicationMenuModule module)
        {
            TreeMenuButton btn = new TreeMenuButton(module);
            btn.Text = module.Menu;
            btn.Dock = DockStyle.Bottom;
            if (module.Image != "")
            {
                System.Reflection.Assembly ass = System.Reflection.Assembly.Load(module.Namespace.Library);
                ResourceManager myManager = new ResourceManager(module.Namespace.Library + ".Properties.Resources", ass);
                try
                {
                    btn.Image = (Image)myManager.GetObject(module.Image);

                    // btn.Image = Image.FromFile(imagePath);
                }
                catch (Exception)
                {
                    MessageBox.Show(string.Concat("Missing ", module.Image));
                }
            }
            btn.ButtonClick += new EventHandler(btn_ButtonClick);
            this.Controls.Add(btn);
        }

        private void btn_ButtonClick(object sender, EventArgs e)
        {
            TreeMenuButton button = (TreeMenuButton)sender;
            treeView1.SuspendLayout();
            treeView1.Nodes.Clear();
            currentNode = null;

            foreach (ApplicationMenuFolder folder in button.MenuModule.MenuFolders)
            {
                TreeNode folderNode = new TreeNode(folder.Folder);
                foreach (ApplicationMenuItem item in folder.MenuItems)
                {
                    TreeMenuNode node = new TreeMenuNode(item);
                    folderNode.Nodes.Add(node);
                }
                treeView1.Nodes.Add(folderNode);
            }
            for (int t = 0; t < treeView1.Nodes.Count; t++)
                treeView1.Nodes[t].ExpandAll();
            treeView1.ResumeLayout();
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.GetType() != typeof(TreeMenuNode))
                return;

            TreeMenuNode node = (TreeMenuNode)e.Node;

            if (NodeClick != null)
                NodeClick(node, new EventArgs());
        }

        private void treeView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && currentNode != null)
            {
                if (currentNode.GetType() == typeof(TreeNode))
                {
                    if (currentNode.IsExpanded)
                        currentNode.Collapse();
                    else
                        currentNode.Expand();
                }
                else
                    if (NodeClick != null)
                        NodeClick(currentNode, new EventArgs());
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            currentNode = e.Node;
        }
    }

    #region TreeMenuButton

    internal class TreeMenuButton : Button
    {
        public event EventHandler ButtonClick;

        private bool selected = false;

        public ApplicationMenuModule MenuModule { get; private set; }

        #region Properties

        public bool Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                DoMouseLeave();
            }
        }

        #endregion

        public TreeMenuButton(ApplicationMenuModule MenuModule)
        {
            base.BackgroundImage = Properties.Resources.Blu;
            base.BackgroundImageLayout = ImageLayout.Stretch;
            base.ImageAlign = ContentAlignment.MiddleLeft;
            base.FlatStyle = FlatStyle.Flat;
            base.FlatAppearance.BorderSize = 0;

            //base.TextImageRelation = TextImageRelation.ImageBeforeText;
            base.Size = new System.Drawing.Size(127, 32);
            base.MinimumSize = new System.Drawing.Size(10, 32);
            this.MenuModule = MenuModule;
        }

        public new Size Size
        {
            get { return base.Size; }
            set
            {
                base.Size = value;
            }
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                this.Invalidate();
            }
        }

        protected override void OnClick(EventArgs e)
        {
            Selected = true;
            Deselect();
            if (ButtonClick != null)
                ButtonClick(this, e);

            base.OnClick(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (Selected)
                BackgroundImage = Properties.Resources.OrangeDark;
            else
                BackgroundImage = Properties.Resources.OrangeLight;
            base.OnMouseClick(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            if (Selected)
                BackgroundImage = Properties.Resources.OrangeDark;
            else
                BackgroundImage = Properties.Resources.OrangeLight;

            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            DoMouseLeave();
            base.OnMouseLeave(e);
        }

        private void DoMouseLeave()
        {
            if (Selected)
                BackgroundImage = Properties.Resources.Orange;
            else
                BackgroundImage = Properties.Resources.Blu;
        }

        private void Deselect()
        {
            foreach (Control ctrl in this.Parent.Controls)
            {
                if (ctrl.GetType() == typeof(TreeMenuButton) && ctrl != this)
                    ((TreeMenuButton)ctrl).Selected = false;
            }
        }
    }

    #endregion

    internal class TreeMenuNode : TreeNode
    {
        public ApplicationMenuItem Item { private set; get; }

        public TreeMenuNode(ApplicationMenuItem item)
            : base()
        {
            Item = item;
            this.Text = item.Title;
            switch (item.DocumentType)
            {
                case ApplicationLibrary.SqlConnector.FormType.Document:
                    this.ImageKey = "Form";
                    this.SelectedImageKey = "Form";
                    break;

                case ApplicationLibrary.SqlConnector.FormType.Batch:
                    this.ImageKey = "Batch";
                    this.SelectedImageKey = "Batch";
                    break;

                case ApplicationLibrary.SqlConnector.FormType.Report:
                    this.ImageKey = "Printer";
                    this.SelectedImageKey = "Printer";
                    break;

                default:
                    this.ImageKey = "Folder";
                    this.SelectedImageKey = "Folder";
                    break;
            }
        }
    }
}